package org.example.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.annotation.OperationLog;
import org.example.dao.OperationLogDao;
import org.example.domain.OperationLogEntity;
import org.example.domain.PageBean;
import org.example.domain.response.ResponseResult;
import org.example.services.OperationLogService;
import org.example.utils.PageConvertPageBeanUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
@Transactional
public class OperationLogServiceImpl implements OperationLogService {

    @Resource
    private OperationLogDao operationLogDao;

    public boolean saveOperationLog(OperationLogEntity operationLogEntity){
        return operationLogDao.insert(operationLogEntity)>0 ? true : false;
    }

    @Override
    public ResponseResult getOperation(int pageSize, int currentPage) {
        try {
            Page<OperationLogEntity> page = new Page<>(currentPage, pageSize);
            LambdaQueryWrapper<OperationLogEntity> discussionLambdaQueryWrapper = new LambdaQueryWrapper<>();
            discussionLambdaQueryWrapper.orderByDesc(OperationLogEntity::getTime);
            Page<OperationLogEntity> discussionPage = operationLogDao.selectPage(page, discussionLambdaQueryWrapper);
            PageBean<OperationLogEntity> pageBean = PageConvertPageBeanUtil.convert(discussionPage);
            log.info("分页查询讨论：成功{}",pageBean);
            return ResponseResult.success("分页查询讨论成功",pageBean);
        } catch (Exception e) {
            e.printStackTrace();
            log.warn("分页查询讨论：失败");
            return ResponseResult.failed("分页查询讨论失败");
        }
    }


}
